<?php
//require_once  __DIR__ . '/includes/authenticate.php';

if (isset($_POST['season']) && isset($_POST['date'])) {	

include_once './db_bb_connect.php';

$season = $_POST['season'];
$date = $_POST['date'];


$sql = "SELECT teams.playerID, users.firstname
	FROM teams 
	JOIN users 
	ON teams.playerID = users.user_key 
	WHERE teams.season = '$season'";


$result = $db->query($sql);

while ($row = $result->fetch_assoc()){

$playerID = $row['playerID'];
$firstname = $row['firstname'];

foreach ($_POST as $key => $value) {
	if ($key == $playerID) {
		$presence = $value;
		$sql2 = "INSERT INTO trainingpresence (playerID, date, presence, season) 
	VALUES ('$playerID','$date','$presence', '$season')";
	if ($db->query($sql2)) {
		//echo "<br><center><strong>Training Presence has been succesfully added! </strong></center>";
	} else {
		echo "<br><center>Error: " . $sql2 . "<br>" . $db->error . "</center>";
		}
	} else if ($key == "comment$playerID") {
			$comment = $value;

			$sql2 = "UPDATE trainingpresence 
			SET comment = '$comment' 
	WHERE playerID = '$playerID' AND date = '$date'";

			if ($db->query($sql2)) {
				//echo "<br><center><strong>Training Presence has been succesfully added! </strong></center>";
			} else {
				echo "<br><center>Error: " . $sql2 . "<br>" . $db->error . "</center>";
			}
		}
	}
}
echo "<br><center><strong>Training Presence has been succesfully added! </strong></center>";

}



?>